package org.myhomework.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public final class JdbcUtil {

    private static String url;
    private static String username;
    private static String password;

    static {
        try {
            // driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            Properties properties = new Properties();
            InputStream resourceAsStream = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
            properties.load(resourceAsStream);
            JdbcUtil.url = properties.getProperty("db.url");
            JdbcUtil.username = properties.getProperty("db.username");
            JdbcUtil.password = properties.getProperty("db.password");
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }

}
